package test.statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class StatementMain {

	public static void main(String[] args) {

		final String TAB = "\t";

		final String URL = "jdbc:sqlite:/Users/ ... /student";
		
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		
		//insert
		String query1 = "insert into student (id, name, degree) values (101, 'Alice', 10)";

		//update
		String query2 = "update student set degree = 11 where id = 111";
		
		//delete
		String query3 = "delete from student where id = 109";
		
		//select
		String query4 = "select * from student where id >= 105 and id < 120";
		
		int count = -1;
		
		try{
			Class.forName("org.sqlite.JDBC");

			conn = DriverManager.getConnection(URL);
			st = conn.createStatement();
			
			//for query1;
			///*
			count = st.executeUpdate(query1);
			System.out.println("query1" + TAB + "count : " + count);
			//*/
			//***************************************

			//for query2;
			///*
			count = st.executeUpdate(query2);
			System.out.println("query2" + TAB + "count : " + count);
			//*/
			//***************************************

			//for query3;
			///*
			count = st.executeUpdate(query3);
			System.out.println("query3" + TAB + "count : " + count);
			//*/
			//***************************************
			
			//for query4
			///*
			rs = st.executeQuery(query4);
			while(rs.next()){
				System.out.println(rs.getString("id") + TAB + rs.getString("name"));
			}
			//*/
			//***************************************
		
		}catch(ClassNotFoundException e1){
			e1.printStackTrace();
		} catch (SQLException e2) {
			//
			e2.printStackTrace();
		}finally{
			try{
				if(rs != null) rs.close();
				if(st != null) st.close();
				if(conn != null) conn.close();
			}catch(Exception e3){
				e3.printStackTrace();
			}
		}
	}
}
